﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Xml.Serialization;
using Microsoft.WowAddonStudio.FrameXml.Serialization.TypeConverters;

namespace Microsoft.WowAddonStudio.FrameXml.Serialization
{
    [Serializable]
    [DesignerCategory("code")]
    [XmlType(Namespace = "http://www.blizzard.com/wow/ui/")]
    [DebuggerDisplay("Value = {Value}")]
    [TypeConverter(typeof(FontHeightTypeConverter))]
    public class FontHeight : SerializationObject, IEquatable<FontHeight>, ICloneable, ICopiable<FontHeight>
    {
        /// <summary>
        /// Gets or sets the <see cref="AbsValue"/> used to express the height of the font.
        /// </summary>
        [XmlElement]
        public AbsValue AbsValue { get; set; }

        /// <summary>
        /// Gets or sets the height of the font. This property is not serialized and wraps the <see cref="AbsValue"/> property.
        /// </summary>
        [XmlIgnore]
        public int? Value
        {
            get { return AbsValue != null ? AbsValue.Value : (int?)null; }
            set { this.AbsValue = value.HasValue ? new AbsValue { Value = value.Value } : null; }
        }

        /// <summary>
        /// Gets a value indicating whether a serialization object is complete and can be rendered.
        /// </summary>
        public override bool IsComplete
        {
            get { return AbsValue != null; }
        }

        public bool Equals(FontHeight obj)
        {
            if (ReferenceEquals(null, obj))
                return false;
            if (ReferenceEquals(this, obj))
                return true;

            return Equals(obj.Value, this.Value);
        }

        /// <summary>
        /// Creates a new object that is a copy of the instance.
        /// </summary>
        /// <returns>The copied instance.</returns>
        /// <remarks>Performs a deep copy.</remarks>
        public FontHeight Copy()
        {
            return new FontHeight
                       {
                           AbsValue = AbsValue != null ? AbsValue.Copy() : null,
                           Value = Value
                       };
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">
        /// The <paramref name="obj"/> parameter is null.
        /// </exception>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
                return false;
            if (ReferenceEquals(this, obj))
                return true;
            if (obj.GetType() != typeof (FontHeight))
                return false;

            return Equals((FontHeight) obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return Value.GetHashCode();
        }

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        public object Clone()
        {
            return Copy();
        }
    }
}
